Welcome to Jupyter Notebook, this is an example of a Python notebook. A quick overview of how notebooks work can be found here.
Coming Up:
In [1]:
import json, shapely, fiona, os
import seaborn as sns
import pandas as pd
import geopandas as gpd
import networkx as nx
import matplotlib.pyplot as plt
%matplotlib inline
In [2]:
# load the sample dataset (iris)
iris = sns.load_dataset('iris')
#look at it:
print("Rows: ",len(iris))
iris.head()
Out[2]:
In [3]:
iris.species.value_counts()
Out[3]:
In [4]:
#Now let's plot it:
ax = iris[iris.species=='setosa'].plot(kind='scatter', x='sepal_length', y='petal_length', color='steelblue', figsize=(10,6))
iris[iris.species=='virginica'].plot(kind='scatter', x='sepal_length', y='petal_length', ax=ax, color='red')
iris[iris.species=='versicolor'].plot(kind='scatter', x='sepal_length', y='petal_length', ax=ax, color='orange')
ax.legend(['Setosa','Virginica','Versicolor'])
Out[4]:
In [5]:
# First we're going to read in the default data
path = gpd.datasets.get_path('naturalearth_lowres')
df = gpd.read_file(path)
df.head()
Out[5]:
In [6]:
print("Average Population per country: {0}".format(df.pop_est.mean().round()))
print("Median Population per country: {0}".format(df.pop_est.median().round()))
In [7]:
#Now let's plot it
df.plot(figsize=(10,8))
Out[7]:
In [8]:
# Get fancier with other libraries
In [9]:
import geoplot
In [10]:
fig, axes = plt.subplots(1,2)
fig.set_size_inches(15,8)
geoplot.cartogram(df[df['continent'] == 'Africa'],
scale='pop_est', limits=(0.2, 1), figsize=(7, 8), ax=axes[0])
geoplot.cartogram(df[df['continent'] == 'South America'],
scale='pop_est', limits=(0.2, 1), figsize=(7, 8), ax=axes[1])
Out[10]:
In [11]:
df = gpd.read_file('data/HydrologyLine.shp')
df.columns = ['layer','length','geometry']
df.head()
Out[11]:
In [12]:
df.plot(figsize=(15,8))
Out[12]:
In [13]:
df.crs
Out[13]:
In [14]:
mercator = df.head(10).to_crs({'init': 'epsg:4326'})
In [15]:
osmp_lands = gpd.read_file('data/OSMPLands.shp')
osmp_lands.head(3)
Out[15]:
In [16]:
ax = osmp_lands[osmp_lands.TYPE=='Conservation Easement'].plot(figsize=(15,8))
osmp_lands[osmp_lands.TYPE!='Conservation Easement'].plot(ax=ax,color='green')
# geoplot.choropleth(osmp_lands, hue='PropertyID', cmap='Greens', figsize=(8, 4))
Out[16]:
In [31]:
crime = gpd.read_file('data/Target_Crime_Locations.shp')
crime['date'] = crime.REPORTDATE.apply(lambda x: pd.Timestamp(x).date())
crime['year'] = crime.date.apply(lambda x: x.year)
crime.head()
Out[31]:
In [32]:
ax = crime.groupby('date').aggregate({"REPORTNUM":"count"}).cumsum().plot()
ax.set_title("Amount of crime reports filed over time");
In [33]:
# Looks like crime is not going up in Boulder?
crime.OFFENSE.value_counts()
Out[33]:
In [34]:
crime[crime.OFFENSE=='Trespassing'].groupby('date').aggregate('count')['OBJECTID'].cumsum().plot(figsize=(15,8))
Out[34]:
In [44]:
conservation_easement = osmp_lands[osmp_lands.TYPE=='Conservation Easement'].to_crs(crime.crs)
In [46]:
ax = conservation_easement.plot(figsize=(15,8))
crime[(crime.OFFENSE=='Trespassing') & (crime.year==2018)].plot(ax=ax, color='red', linewidth=0.01)
Out[46]:
In [105]:
crimes_on_osmplands = gpd.sjoin(osmp_lands.to_crs(crime.crs),crime) #Spatial join Points to polygons
crimes_on_osmplands.head(2)
Out[105]:
In [108]:
# What crimes happpen on OSMP lands?
crimes_on_osmplands.OFFENSE.value_counts()
Out[108]:
In [82]:
prairie = gpd.read_file('data/prairies.shp')
print("Found {0} colonies".format(len(prairie)))
prairie.head()
Out[82]:
In [93]:
prairie.head().buffer(0.001).plot()
Out[93]: